﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text ;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_PRODUCT.Product.WTS
{
    public partial class frm_MachineWTSFind : DevExpress.XtraEditors.XtraForm
    {
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();
        ClsMachineWTSTable b = new ClsMachineWTSTable();
        DataTable dtStaff = new DataTable();
        DataTable dtMOno = new DataTable();
        ctrlMachineWTSDetail ctrl = new ctrlMachineWTSDetail();
        private bool flagLoadDone;
        private bool hasAnd;
        string Where = "where";
        public frm_MachineWTSFind(ctrlMachineWTSDetail ctrlMWTS)
        {
            InitializeComponent();
            this.ctrl = ctrlMWTS;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            string sql = string.Empty;
            
            if (dateDateFrom.EditValue != null && dateDateTo.EditValue == null)
            {
                Where = Where + " convert(Date,[242_MachineWTS].date,111) = '" + dateDateFrom.DateTime.ToString("yyyy/MM/dd") + "' ";
                hasAnd = true;
            }
            else if (dateDateFrom.EditValue != null && dateDateTo.EditValue != null)
            {
                Where = Where + " convert(Date,[242_MachineWTS].date,111) >= '" + dateDateFrom.DateTime.ToString("yyyy/MM/dd") + "' and  convert(Date,[242_MachineWTS].date,111) <= '" + dateDateTo.DateTime.ToString("yyyy/MM/dd") + "'";
                hasAnd = true;
            }

            if (cbMachine.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].MachineID = '" + cbMachine.SelectedValue.ToString() + "'";

                }
                else
                {
                    Where = Where + " [242_MachineWTS].MachineID = '" + cbMachine.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbMONo.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].MONo = '" + cbMONo.Text.Trim() .ToString() + "'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].MONo = '" + cbMONo.Text.Trim() .ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbOption.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].OptionID = '" + cbOption.Text.Trim() .ToString() + "'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].OptionID = '" + cbOption.Text.Trim() .ToString() + "'";

                    hasAnd = true;
                }

            }

            if (cbStaff.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].StaffID = '" + cbStaff.SelectedValue.ToString() + "'";
                }
                else
                {
                    Where = Where + "  [242_MachineWTS].StaffID = '" + cbStaff.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbWorkID.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].WorkID = '" + cbWorkID.SelectedValue.ToString() + "'";
                }
                else
                {
                    Where = Where + "  [242_MachineWTS].WorkID = '" + cbWorkID.SelectedValue.ToString() + "'";
                    hasAnd = true;
                }
            }

            if (cbPartID.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MO].PartID = '" + cbPartID.Text.Trim()  + "'";
                }
                else
                {
                    Where = Where + "  [242_MO].PartID = '" + cbPartID.Text.Trim()  + "'";
                    hasAnd = true;
                }
            }

            if (cbShift.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].Shift = '" + cbShift.SelectedItem + "'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].Shift = '" + cbShift.SelectedItem + "'";
                    hasAnd = true;
                }
            }

            if (txtTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].Time = '" + txtTime.Text.Trim() + "'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].Time = '" + txtTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtOKQty.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtOKQty.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_MachineWTS].OKQty = '0' or (ISNULL([242_MachineWTS].OKQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_MachineWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    }
                    //Where = Where + " and [242_MachineWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                }
                else
                {
                    if (txtOKQty.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_MachineWTS].OKQty = '0' or (ISNULL([242_MachineWTS].OKQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_MachineWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    }
                    //Where = Where + " [242_MachineWTS].OKQty = '" + txtOKQty.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtNGQty.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtNGQty.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_MachineWTS].NGQty = '0' or (ISNULL([242_MachineWTS].NGQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_MachineWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    }
                    //Where = Where + " and [242_MachineWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                }
                else
                {
                    if (txtNGQty.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_MachineWTS].NGQty = '0' or (ISNULL([242_MachineWTS].NGQty, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_MachineWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    }
                    //Where = Where + " [242_MachineWTS].NGQty = '" + txtNGQty.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtProTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtProTime.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_MachineWTS].ProTime = '0' or (ISNULL([242_MachineWTS].ProTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_MachineWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    }
                }
                else
                {
                    if (txtProTime.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_MachineWTS].ProTime = '0' or (ISNULL([242_MachineWTS].ProTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_MachineWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    }

                    //Where = Where + " [242_MachineWTS].ProTime = '" + txtProTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtClampTime.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    if (txtClampTime.Text.Trim() == "0")
                    {
                        Where = Where + " and ([242_MachineWTS].ClampTime = '0' or (ISNULL([242_MachineWTS].ClampTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + " and [242_MachineWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    }

                  //  Where = Where + " and [242_MachineWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                }
                else
                {
                    if (txtClampTime.Text.Trim() == "0")
                    {
                        Where = Where + "  ([242_MachineWTS].ClampTime = '0' or (ISNULL([242_MachineWTS].ClampTime, '') = ''))";
                    }
                    else
                    {
                        Where = Where + "  [242_MachineWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    }

                    //Where = Where + " [242_MachineWTS].ClampTime = '" + txtClampTime.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            if (txtNote1.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].Note1 like '%" + txtNote1.Text.Trim() + "%'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].Note1 like '%" + txtNote1.Text.Trim() + "%'";
                    hasAnd = true;
                }
            }

            if (txtNote2.Text.Trim() != "")
            {
                if (hasAnd)
                {
                    Where = Where + " and [242_MachineWTS].Note2 = '" + txtNote2.Text.Trim() + "'";
                }
                else
                {
                    Where = Where + " [242_MachineWTS].Note2 = '" + txtNote2.Text.Trim() + "'";
                    hasAnd = true;
                }
            }

            sql = "SELECT      [242_MachineWTS].ID,[242_MachineWTS].Date, [242_MachineWTS].Shift, [242_MachineWTS].MachineID, [242_MachineWTS].StaffID, [242_MachineWTS].MONo, [242_MachineWTS].OptionID, " +
                " [242_MachineWTS].WorkID, [242_MachineWTS].Time, [242_MachineWTS].OKQty, [242_MachineWTS].NGQty, " +
                " CASE [242_MachineWTS].ProTime WHEN 0 THEN NULL ELSE [242_MachineWTS].ProTime END AS ProTime, " +
                " CASE [242_MachineWTS].ClampTime WHEN 0 THEN NULL  ELSE [242_MachineWTS].ClampTime END AS ClampTime, " +
                " [242_MachineWTS].Note1, [242_MachineWTS].Note2, [242_MO].PartID, [242_MachineWTS].[From], " +
                " [242_MachineWTS].[to], [242_MachineWTS].SProTime, [242_MachineWTS].SClampTime, [242_MachineWTS].TOKQty, [242_MachineWTS].TNGQty " +
                " FROM          [242_MO] RIGHT OUTER JOIN " +
                "                [242_MachineWTS] ON [242_MO].MONo = [242_MachineWTS].MONo  " +
                Where +
                " ORDER BY [242_MachineWTS].ID, [242_MachineWTS].Date, [242_MachineWTS].Shift, [242_MachineWTS].MachineID";
            if (Where.Equals("where"))
            {
                MessageBox.Show("Hãy chọn ít nhất một điều kiện tìm kiếm.");

            }
            else
            {
                ctrl.BindDatatoGridFind(BaseDAO.ExecuteQuery(sql));
                Where = "where";
                hasAnd = false;
            }
            Cursor.Current = Cursors.Default;

        }

        private void frm_MachineWTSFind_Load(object sender, EventArgs e)
        {
            //list machine
            DataTable dt3;
            dt3 = clsWTSDAO.GetAllMachine();
            DataRow r3 = dt3.NewRow();
            r3["MachineID"] = "";
            dt3.Rows.InsertAt(r3, 0);
            cbMachine.DataSource = dt3;
            cbMachine.DisplayMember = "MachineID";
            cbMachine.ValueMember = "MachineID";

            //list MO Number
            //DataTable dt2;
            dtMOno = clsWTSDAO.GetAllMONo();
            DataRow r1 = dtMOno.NewRow();
            r1["MONo"] = "";
            r1["PartID"] = "";
            r1["FullName"] = "";
            dtMOno.Rows.InsertAt(r1, 0);
            cbMONo.DisplayMember = "MONo";
            cbMONo.ValueMember = "MONo";



            //list staff
            //DataTable dt4;
            dtStaff = clsWTSDAO.GetAllStaff();
            DataRow r4 = dtStaff.NewRow();
            r4["FullName"] = "";
            r4["StaffID"] = "";
            dtStaff.Rows.InsertAt(r4, 0);
            cbStaff.DataSource = dtStaff;
            cbStaff.DisplayMember = "StaffID";
            cbStaff.ValueMember = "StaffID";

            //list work
            DataTable dt1 = new DataTable();
            dt1 = clsWTSDAO.GetAllWorkMWTS();
            DataRow r2 = dt1.NewRow();
            r2["WorkID"] = "";
            dt1.Rows.InsertAt(r2, 0);
            cbWorkID.DataSource = dt1;
            cbWorkID.DisplayMember = "WorkID";
            cbWorkID.ValueMember = "WorkID";

            flagLoadDone = true;
        }

        private void cbMONo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flagLoadDone)
            {
                //list  OptionID
                DataTable dtOption = new DataTable();
                dtOption = clsWTSDAO.GetAllOptionID(cbMONo.SelectedValue.ToString());
                DataRow r5 = dtOption.NewRow();
                r5["OptionID"] = "";
                dtOption.Rows.InsertAt(r5, 0);
                cbOption.DataSource = dtOption;
                cbOption.DisplayMember = "OptionID";
                cbOption.ValueMember = "OptionID";
            }

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}